package com.market.service.impl;

import com.market.entity.*;
import com.market.entity.SellerCashFlow;
import com.market.mapper.BackStageMapper;
import com.market.service.*;
import com.market.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName Bz
 * @Description TODO
 * @Author 1
 * @Date 2022/4/2 21:50
 * @Version 1.0
 */
@Service
public class BackStageServiceImpl implements BackstageService {
    @Autowired
    BackStageMapper backStageMapper;
    @Autowired
    BuyerService buyerService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    GoodsTypeService goodsTypeService;
    @Autowired
    OrderService orderService;
    @Autowired
    SendBackService sendBackService;
    @Autowired
    IntoStorageService intoStorageService;
    @Autowired
    BuyerCashFlowService buyerCashFlowService;
    @Autowired
    StoreService storeService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    SortRecordService sortRecordService;
    //添加后台用户
    @Override
    public boolean addBackStageUser(Backstage backstage) {
        return backStageMapper.addBackStageUser(backstage)>0;
    }




    //根据id查询某个后台人员信息
    @Override
    public Backstage queryBackstage(int backstageId) {
        return backStageMapper.queryBackstage(backstageId);
    }


    //姓名和账号查询后台人员信息
    @Override
    public Backstage queryBackstageByNameAndAccount(String name, String account) {
        return backStageMapper.queryBackstageByNameAndAccount(name,account);
    }

    //查询所有后台人员
    @Override
    public List<Backstage> queryAllBackstage() {
        return backStageMapper.queryAllBackstage();
    }

    //根据id查询某个买家
    @Override
    public Buyer queryBuyerInfo(int buyerId) {
        return buyerService.queryBuyerById(buyerId);
    }

    //查询所有买家
    @Override
    public List<Buyer> queryAllBuyer() {
        return buyerService.queryAllBuyer();
    }

    //锁定买家
    @Override
    public boolean lockBuyer(int buyerId) {
        return buyerService.lockBuyer(buyerId);
    }

    //解锁买家
    @Override
    public boolean unLockBuyer(int buyerId) {
        return buyerService.unLockBuyer(buyerId);
    }

    //锁定后台用户
    @Override
    public boolean lockBackstage(int backstageId) {
        return backStageMapper.lockBackstage(backstageId)>0;
    }

    //解锁后台用户
    @Override
    public boolean unLockBackstage(int backstageId) {
        return backStageMapper.unLockBackstage(backstageId)>0;
    }

    //根据id修改某个后台人员权限
    @Override
    public boolean updateRole(int backstageId, int newRole) {
        return backStageMapper.updateRole(backstageId,newRole)>0;
    }

    //新增商品
    @Override
    public boolean addGoods(Goods goods) {
        return goodsService.addGoods(goods);
    }

    //上架商品
    @Override
    public boolean shelveGoods(int goodsId) {
        return goodsService.shelveGoods(goodsId);
    }

    //下架商品
    @Override
    public boolean offShelveGoods(int goodsId) {
        return goodsService.offShelveGoods(goodsId);
    }

    //删除商品
    @Override
    public boolean deleteGoods(int goodsId) {
        return goodsService.deleteGoods(goodsId);
    }

    //根据商品id查询商品
    @Override
    public Goods queryGoodsByGoodsId(int goodsId) {
        return goodsService.queryGoodsByGoodsId(goodsId);
    }

    //添加商品类别(调用商品类别的方法)
    @Override
    public boolean addGoodsType(String typeName) {
        return goodsTypeService.addGoodsType(typeName);
    }

    //修改商品属性（名称，售价，描述，每日可销售数量）
    @Override
    public boolean updateGoodsProperty(int goodsId, String goodsName, double price, String description, int typeId,int sellNum) {
        return goodsService.updateGoodsProperty(goodsId,goodsName,price,description,typeId,sellNum);
    }

    //修改商品可销售数量
    public boolean updateGoodsSellNums(int goodsId,int newSellNum){
        return goodsService.updateGoodsSellNums(goodsId,newSellNum);
    }

    //查询所有订单（调用订单的方法）
    @Override
    public List<Order> queryAllOrder() {
        return orderService.queryAllOrders();
    }

    // 根据订单编号查询某个订单
    @Override
    public Order queryOrderByOrderCode(String orderCode) {
        return orderService.queryOrderByOrderCode(orderCode);
    }

    // 根据订单id查询某个订单
    @Override
    public Order queryOrderByOrderId(int orderId) {
        return orderService.queryOrderByOrderId(orderId);
    }


    //查询某个买家的全部订单
    @Override
    public List<Order> queryOrderByBuyerId(int buyerId) {
        List<Order> orders = orderService.queryAllBuyerOrders(buyerId);
        return orders;
    }


    //删除某个订单
    @Override
    public boolean deleteOrder(int orderId) {
        return orderService.deleteOrder(orderId);
    }


    //查询今天已付款的订单
    @Override
    public List<Order> queryTodayAlreadyPaidOrder(String createTime) {
        List<Order> orders = orderService.queryDayOrders(createTime);
        return orders;
    }

    @Override
    public Backstage queryBackstageByAccount(String account) {
        return backStageMapper.queryBackStageByAccount(account);
    }

    //查找所有状态为2的后台人员
    @Override
    public List<Backstage> queryBackstageByState() {
        return backStageMapper.queryBackstageByState();
    }

    //删除后台人员
    @Override
    public boolean delBackstage(int backId) {
        return backStageMapper.delBackstage(backId);
    }

    //查看某个买家资金流水
    @Override
    public List<BuyerCashFlow> queryBuyerCashFlow(int buyerId) {
        return buyerCashFlowService.queryAllBuyerCashFlow(buyerId);
    }

    //审核是否与小店合作
    @Override
    public boolean checkStore(int storeId,int newState) {
        return storeService.modifyState(storeId,newState);
    }

    //商品入库
    @Override
    public boolean goodsIntoStorage(int goodsId,String backStageName) {
        IntoStorage intoStorage=new IntoStorage();
        Goods goods=goodsService.queryGoodsByGoodsId(goodsId);
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String payDay=sdf.format(date);
        int dayAllNum=orderDetailService.GoodsAllBuyAmount(goodsId,payDay);
        intoStorage.setGoodsId(goodsId);
        intoStorage.setIntoStorageTime(date);
        intoStorage.setNeedIntoStorageNum(dayAllNum);
        intoStorage.setPurchasePrice(goods.getPrice());
        intoStorage.setPerson(backStageName);
        return intoStorageService.addIntoStorageRecord(intoStorage);
    }

    //查看某个退货单
    @Override
    public SendBack querySendBack(int sendBackId) {
        return sendBackService.findSendBack(sendBackId);
    }

    //查询某个买家的所有退货单
    @Override
    public List<SendBack> queryAllSendBack(int buyerId) {
        return sendBackService.findSendBackRecords(buyerId);
    }

    //删除某个退货单
    @Override
    public boolean deleteSendBack(int sendBackId) {
        return sendBackService.deleteSendBack(sendBackId);
    }

    //审核退货
    @Override
    public boolean checkSendBackGoods(int sendBackId,int state) {
        return sendBackService.modifySendBackState(sendBackId,state);
    }

    //审核换货
    @Override
    public boolean checkExchange(int orderId) {
        return false;
    }

    //打印某天分拣单
    @Override
    public List<SortRecord> printTodaySortRecord(String day) {
        return sortRecordService.daySortRecord(day);
    }

    //todo 查看所有卖家资金流水
    @Override
    public List<SellerCashFlow> querySellerCashFlow() {
        return null;
    }

    //todo 查看所有小店资金流水
    @Override
    public List<Store> queryStoreCashFlow(int storeId) {
        return null;
    }

}
